package com.yanzhu.api;

import com.yanzhu.bean.ResponseBean;
import com.yanzhu.constant.Token;
import com.yanzhu.service.UserService;
import com.yanzhu.service.WalletService;
import com.yanzhu.service.model.User;
import com.yanzhu.service.model.Wallet;
import com.yanzhu.util.WechatUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @Author: 方炎柱
 * @Description: 用户模块
 * @Date: Created in 10:01 2018/1/22
 * @Modified:
 */
@AllArgsConstructor
@RestController
public class UserAPI {

    private UserService userService;

    private WalletService walletService;

    @RequestMapping(value = "/queryUserInfo")
    public ResponseBean queryUserInfo(HttpServletRequest request) throws Exception {
        String uid = request.getParameter("uid");
        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数!");

        User user = userService.queryUserInfo(Long.parseLong(uid));
        if(user == null)
            return ResponseBean.fail("用户不存在!");

        user.setNickname(URLDecoder.decode(user.getNickname(), "utf-8"));
        return ResponseBean.ok(user);
    }

    //查询余额
    @RequestMapping(value = "/queryUserWallet")
    public ResponseBean queryUserWallet(HttpServletRequest request){
        String uid = request.getParameter("uid");
        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数!");

        Wallet wallet = walletService.selectBalanceByUid(Long.parseLong(uid));
        return ResponseBean.ok(wallet != null? wallet.getBalance():0.0);
    }

    @RequestMapping(value = "/wx_login")
    public ResponseBean wx_login(HttpServletRequest request) throws Exception {
        String code  = request.getParameter("code");
        if(code == null || code.equals(""))
            return ResponseBean.fail("缺少code参数！");

        Token token  = WechatUtil.getToken(code);
        if(token == null)
            return ResponseBean.fail("出错了，未获取到Token信息！");

        User user = userService.selectByOpenId(token.getOpenid());
        if(user == null) {
//            user = WechatUtil.getUserInfo(token.getOpenid(), token.getAccess_token());
//            if (user == null)
//                return ResponseBean.fail("请求失败！");
            user = new User();
            user.setOpenId(token.getOpenid());
            user.setNickname("无");
            userService.insert(user);
        }

        return ResponseBean.ok(user);
    }


    //
    @RequestMapping(value = "/addUserInfo")
    public ResponseBean addUserInfo(@RequestBody User user) throws Exception {
//        String uid = request.getParameter("uid");
//        if(uid == null || uid.equals(""))
//            return ResponseBean.fail("缺少用户ID参数!");
//
//        User user = userService.queryUserInfo(Long.parseLong(uid));
//        if(user == null)
//            return ResponseBean.fail("用户ID无效!");
//
//        String nickname = request.getParameter("nickname");
//        if(nickname == null || nickname.equals(""))
//            return ResponseBean.fail("缺少用户昵称参数!");
//
//        String avatar = request.getParameter("avatarUrl");
//        if(avatar == null || avatar.equals(""))
//            return ResponseBean.fail("缺少用户头像参数!");
//
//        String gender = request.getParameter("gender");
//        if(gender == null || gender.equals(""))
//            return ResponseBean.fail("缺少用户性别参数!");
//
//        String city = request.getParameter("city");
//        String province = request.getParameter("province");
//        String country = request.getParameter("country");
//        String language = request.getParameter("language");
//
//        user.setNickname(nickname);
//        user.setAvatar(avatar);
//        user.setGender(Integer.parseInt(gender));
//        if(!(city == null || city.equals("")))
//            user.setCity(city);
//        if(!(province == null || province.equals("")))
//            user.setProvince(province);
//        if(!(country == null || country.equals("")))
//            user.setCountry(country);
//        if(!(language == null || language.equals("")))
//            user.setLanguage(language);

        if(user.getId() == null)
            return ResponseBean.fail("缺少用户ID参数!");

        if(userService.queryUserInfo(user.getId()) == null)
            return ResponseBean.fail("用户ID无效!");

        if(user.getNickname() == null || user.getNickname().equals(""))
            return ResponseBean.fail("缺少用户昵称参数!");

        String nickname = URLEncoder.encode(user.getNickname(), "utf-8");
        user.setNickname(nickname);

        if(user.getAvatar() == null || user.getAvatar().equals(""))
            return ResponseBean.fail("缺少用户头像参数!");

        userService.update(user);

        user.setNickname(URLDecoder.decode(nickname, "utf-8"));
        return ResponseBean.ok(user);
    }
}
